package org.mao.coque.websocket;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.Map;

public class CoqueWebSocketHandler implements WebSocketHandler {

    public static Map<String, WebSocketSession> sessionMap = new HashMap<String, WebSocketSession>();

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("websocket connection closed......");
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("connect to the websocket success......");
        // session.sendMessage(new TextMessage("Server:connected OK!"));
    }

    @Override
    public void handleMessage(final WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        System.out.println(message.getPayload());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable e) throws Exception {
        if (session.isOpen()) {
            session.close();
        }
        System.out.println("websocket connection error......");
    }

    @Override
    public boolean supportsPartialMessages() {
        // TODO Auto-generated method stub
        return false;
    }

}
